<?php
protectScript('ProjectModel.php');

class ProjectModel{
	private static $_asmReadOnlyProps = array(
		'sTableName' => 'project',
		'sTablePrefix' => TABLE_PREFIX
	);

	private $_asmWritableProps = array(
		'iIdProject' => 0,
		'iDateBeginProject' => 0,
		'iDateEndProject' => 0,
		'sLabelProject' => '',
		'sDescriptionProject' => '',
		'iStatusProject' => 1
	);

	const TODO = 1;
	const DONE = 2;
	const DOIN = 3;

	public function __construct($asmProjectInfo = null){
		$this->_asmWritableProps['iIdProject'] = (isset($asmProjectInfo['idProject'])) ? $asmProjectInfo['idProject'] : $this->_asmWritableProps['iIdProject'];
		$this->_asmWritableProps['iDateBeginProject'] = (isset($asmProjectInfo['dateBeginProject'])) ? $asmProjectInfo['dateBeginProject'] : $this->_asmWritableProps['iDateBeginProject'];
		$this->_asmWritableProps['iDateEndProject'] = (isset($asmProjectInfo['dateEndProject'])) ? $asmProjectInfo['dateEndProject'] : $this->_asmWritableProps['iDateEndProject'];
		$this->_asmWritableProps['sLabelProject'] = (isset($asmProjectInfo['labelProject'])) ? $asmTodoListInfo['labelProject'] : $this->_asmWritableProps['sLabelProject'];
		$this->_asmWritableProps['sDescriptionProject'] = (isset($asmProjectInfo['descriptionProject'])) ? $asmProjectInfo['descriptionProject'] : $this->_asmWritableProps['sDescriptionProject'];
		$this->_asmWritableProps['iStatusProject'] = (isset($asmProjectInfo['statusProject'])) ? $asmProjectInfo['statusProject'] : $this->_asmWritableProps['iStatusProject'];
	}

	public function getProjects($sWhereClause = null){
		$oMysqliInstance = AYSMysqli::getInstance();

		if(isset($sWhereClause)){
			$sWhere = "$sWhereClause";
		}else{
			$sWhere = '';
		}
		$query = sprintf(
			'SELECT idProject, dateBeginProject, dateEndProject, labelProject, descriptionProject, statusProject ' .
			"FROM %s " . $sWhere,
			self::$_asmReadOnlyProps['sTablePrefix'].self::$_asmReadOnlyProps['sTableName']
		);
		$rQueryResult = $oMysqliInstance->query($query);

		$aiasmProjectsList = array();
		while($r = $rQueryResult->fetch_assoc()){
			$aiasmProjectsList[] = $r;
		}
		return $aiasmProjectsList;
	}

	public function updateStatus($iIdProject, $iProjectStatus){
		$oMysqliInstance = AYSMysqli::getInstance();
		$query = sprintf(
			"UPDATE %s " .
			"SET statusProject = %d" .
			"WHERE idProject = %d",
			self::$_asmReadOnlyProps['sTablePrefix'].self::$_asmReadOnlyProps['sTableName'],
			$iProjectStatus,
			$iIdProject
		);
		$oMysqliInstance->query($query);
	}

	public function updateDate($iIdProject, $sTypeDate){
		$oMysqliInstance = AYSMysqli::getInstance();
		$query = sprintf(
			"UPDATE %s " .
			"SET %s = NOW()" .
			"WHERE idProject = %d",
			self::$_asmReadOnlyProps['sTablePrefix'].self::$_asmReadOnlyProps['sTableName'],
			$sTypeDate,
			$iIdProject
		);
		$oMysqliInstance->query($query);
	}

	public static function addProject($assProjectInfos){
		$oMysqliInstance = AYSMysqli::getInstance();
		$query = sprintf(
			"INSERT INTO %s (labelProject, descriptionProject, statusProject)".
			"VALUES ('%s', '%s', %d)",
			self::$_asmReadOnlyProps['sTablePrefix'].self::$_asmReadOnlyProps['sTableName'],
			$oMysqliInstance->escape_string($assProjectInfos['labelProject']),
			$oMysqliInstance->escape_string($assProjectInfos['descriptionProject']),
			$assProjectInfos['statusProject']
		);
		$oMysqliInstance->query($query);
	}

	public function editProject($assProjectInfos){
		$oMysqliInstance = AYSMysqli::getInstance();
		$query = sprintf(
			"UPDATE %s ".
			"SET labelProject = '%s', descriptionProject = '%s', statusProject = %d ".
			"WHERE idProject = %d",
			self::$_asmReadOnlyProps['sTablePrefix'].self::$_asmReadOnlyProps['sTableName'],
			$oMysqliInstance->escape_string($assProjectInfos['labelProject']),
			$oMysqliInstance->escape_string($assProjectInfos['descriptionProject']),
			$assProjectInfos['statusProject'],
			$assProjectInfos['idProject']
		);

		$oMysqliInstance->query($query);
	}

	public static function delProject($iIdProject){
		$oMysqliInstance = AYSMysqli::getInstance();
		$query = sprintf(
			"DELETE FROM %s ".
			"WHERE idProject = %d",
			self::$_asmReadOnlyProps['sTablePrefix'].self::$_asmReadOnlyProps['sTableName'],
			$iIdProject
		);
		$oMysqliInstance->query($query);
	}

	public function __get($sVariableName){
		if(isset($this->_asmReadOnlyProps[$sVariableName])){
			if(is_array($this->_asmReadOnlyProps[$sVariableName])){
				return clone new ArrayObject($this->_asmReadOnlyProps[$sVariableName]);
			}elseif(is_object($this->_asmReadOnlyProps[$sVariableName])){
				return clone $this->_asmReadOnlyProps[$sVariableName];
			}else{
				return $this->_asmReadOnlyProps[$sVariableName];
			}
		}elseif(!isset($this->_asmWritableProps[$sVariableName])){
			$this->_asmWritableProps[$sVariableName] = NULL;
		}

		return $this->_asmWritableProps[$sVariableName];
	}

	public function __set($sVariableName, $mValueVariable){
		if(isset($this->_asmReadOnlyProps[$sVariableName])){
			throw new SetReadOnlyPropsException(
				'Tried to set a read only property on the event object',
				ERROR,
				array(
					'scriptName' => 'ProjectModel.php',
					'variableName' => $sVariableName
				)
			);
		}

		return $this->_asmWritableProps[$sVariableName] = $mValueVariable;
	}
}
